package com.red.wood.module.ircs.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.red.wood.common.domain.Page;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.module.ircs.dao.IrcsPublicDomainInfoDao;

@Repository
public class IrcsPublicDomainInfoDaoImpl implements IrcsPublicDomainInfoDao {

	private static final Log log = LogFactory.getLog(IrcsPublicDomainInfoDaoImpl.class);
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public Map<String, Object> getPageData(Map<String, Object> param) {
		String count = "0";
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		list = listPageData(param);
		count = listCount(param);
		
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("totalCount", count);
		rs.put("pageData", list);
		return rs;
	}
	
	public List<Map<String, Object>> listPageData(Map<String, Object> param){
		Page page = (Page) param.get("page");
		int offset = (page.getPageNo() - 1) * page.getPageSize();
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(param.get("loginUser")));
		String userLevel = CommonUtils.valueOf(param.get("userLevel"));
		StringBuffer sql = new StringBuffer();
		if(StringUtils.equals(userLevel,"1")) {
			sql = getSql();
			sql.append(" and ircs.province = '"+userProvince+"'");
			sql.append(getQueryConf(param));
		}else if(StringUtils.equals(userLevel, "2")) {
			sql = getSql();
			sql.append(" and ircs.province = '"+userProvince+"'");
			sql.append(getQueryConf(param));
		}else {
			sql.append("select * from (");
			sql.append(getSql());
			sql.append(" and ircs.province = '"+userProvince+"'");
			sql.append(getQueryConf(param));
			sql.append(" union all ");
			sql.append(getSql());
			sql.append(" and ircs.province != '"+userProvince+"'");
			sql.append(getQueryConf(param));
			sql.append(") a");
		}
		sql.append(" limit " + page.getPageSize() + " offset " + offset+ " ;");
		log.info(sql);
		return jdbcTemplate.queryForList(sql.toString());
	}
	
	public StringBuffer getSql() {
		StringBuffer sql = new StringBuffer();
		sql.append("select t.ircsId,ircs.ircsName,t.domainName,t.domainLevel");
		sql.append(" from t_ircs_publicDomainInfo t inner join t_ircs_ircsdata ircs");
		sql.append(" on t.ircsId = ircs.ircsId");
		sql.append(" where 1 = 1");
		return sql;
	}

	public String listCount(Map<String, Object> param) {
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(param.get("loginUser")));
		String userLevel = CommonUtils.valueOf(param.get("userLevel"));
		StringBuffer sql = new StringBuffer();
		if(StringUtils.equals(userLevel,"1")) {
			sql.append("select count(*) count from (");
			sql.append(getSql());
			sql.append(" and ircs.province = '"+userProvince+"'");
			sql.append(getQueryConf(param));
			sql.append(") a");
		}else if(StringUtils.equals(userLevel, "2")) {
			sql.append("select count(*) count from (");
			sql.append(getSql());
			sql.append(" and ircs.province = '"+userProvince+"'");
			sql.append(getQueryConf(param));
			sql.append(") a");
		}else {
			sql.append("select count(*) count from (");
			sql.append(getSql());
			sql.append(" and ircs.province = '"+userProvince+"'");
			sql.append(getQueryConf(param));
			sql.append(" union all ");
			sql.append(getSql());
			sql.append(" and ircs.province != '"+userProvince+"'");//公共域名无法区分本省与外省
			sql.append(getQueryConf(param));
			sql.append(") a");
		}
		Map<String, Object> map = jdbcTemplate.queryForList(sql.toString()).get(0);
		String count = map.get("count").toString();
		return count;
	}
	
	public StringBuffer getQueryConf(Map<String,Object> param) {
		String domainName = CommonUtils.valueOf(param.get("domainName"));
		String domainLevel = CommonUtils.valueOf(param.get("domainLevel"));
		String ircsId = CommonUtils.valueOf(param.get("ircsId"));
		StringBuffer queryConf = new StringBuffer();
		if(StringUtils.isNotBlank(ircsId)) {
			queryConf.append(" and t.ircsId='"+ircsId+"'");
		}
		if(StringUtils.isNotBlank(domainName)) {
			queryConf.append(" and t.domainName like '%"+domainName+"%'");
		}
		if(StringUtils.isNotBlank(domainLevel)) {
			queryConf.append(" and t.domainLevel like '%"+domainLevel+"%'");
		}
		return queryConf;
	}
}
